<?php

namespace App\Http\Requests\Api;

class CompanyJobRequest extends ApiRequest
{

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'id' => $this->is('*/update') ? 'bail|required|integer' : '',
            'com_id' => $this->is('*/create') ? 'bail|required|integer' : '',
            'job_title' => 'required',
            'job_category' => 'required',
            'industry' => 'bail|required|integer',
            'location' => 'required',
            'department' => 'integer',
            'recruiting_num' => 'bail|required|integer|min:0',
            'salary_above' => 'bail|required|integer|between:0,1000000',
            'salary_below' => 'bail|required|integer|max:1000000|min:' . $this->salary_above,
            'description' => 'bail|required|between:3,1000',
            'education' => 'bail|required|integer',
            'work_year' => 'bail|required|integer',
            'age_above' => 'bail|required|integer|min:0',
            'age_below' => 'bail|required|integer|' . (($this->age_below != 0) ? ("min:" . $this->age_above) : ("")),
            'gender' => 'bail|required|integer|between:0,2',
            'language' => 'bail|required|integer',
            'part_status' => 'bail|required|integer|between:1,4',
            'contact_info' => 'bail|required|integer|between:0,3',
            'welfare' => 'required',
            'contact.contacts' => 'required_if:contact_info,2',
            'contact.mobile' => 'bail|required_if:contact_info,2|mobile_verification',
            'contact.phone' => 'bail|required_if:contact_info,2|check_company_phone',
            'contact.fax' => 'check_fax',
            'contact.email' => 'email',
        ];
    }

    /** 修改验证的字段值
     * @return array
     */
    protected function validationData()
    {
        $data = parent::validationData();
        $contact = $this->input('contact', []);

        if (is_string($contact)) {
            $v = explode(',', $contact);
            $k = ['contacts', 'mobile', 'phone', 'fax', 'email'];
            $data['contact'] = array_combine($k, $v);
        }
        return $data;
    }

    /*
     * 字段描述
     */
    public function attributes()
    {
        return [
            'job_title' => '职务名称',
            'job_category' => '岗位类别',
            'job_category.*' => '岗位类别',
            'industry' => '所属行业',
            'location' => '工作地点',
            'location.*' => '工作地点',
            'department' => '招聘部门',
            'recruiting_num' => '招聘人数',
            'salary_above' => '最低薪资',
            'salary_below' => '最高薪资',
            'description' => '描述',
            'education' => '学历',
            'work_year' => '工作经验',
            'age_above' => '最小年龄',
            'age_below' => '最大年龄',
            'gender' => '性别',
            'language' => '语言',
            'contact_info' => '独立联系方式',
            'welfare' => '福利',
            'contact.contacts' => '联系人',
            'contact.mobile' => '手机号码',
            'contact.phone' => '固定电话',
            'contact.fax' => '传真',
            'contact.email' => '电子邮件',
            'part_status' => '兼职状态',
        ];
    }

    public function messages()
    {
        return array_merge(parent::messages(), [
            'contact.mobile.mobile_verification' => '请输入正确的手机号码',
            'contact.phone.check_company_phone' => '请输入正确的区号和电话号码',
            'contact.fax.check_fax' => '请输入正确的传真号码',
            'salary_below.min' => '最高薪资不能小于最低薪资',
            'age_below.min' => '最大年龄要求不能小于最小年龄',
            'description.between' => ':attribute不能小于:min个字',
        ]);
    }
}
